package com.chaoxing.controller;

import com.chaoxing.entity.Notebook;
import com.chaoxing.entity.User;
import com.chaoxing.service.NotebookService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class NotebookController {

    @Resource
    private NotebookService notebookService;
    //  给表中插入一篇文章
    @RequestMapping("/insertSelective")
    public Map<String,Integer> insertSelective(Notebook record, HttpServletRequest request){
        Map<String,Integer> map = new HashMap<>();
        String msg = "保存失败!";
        Integer i = notebookService.insertSelective(record,request);
        if (i == 1){
            msg = "保存成功!";
            map.put(msg,i);
        }
        map.put(msg,i);
        return map;
    }

    @RequestMapping("/queryNoteBook")
    public Map<String, List<Notebook>> queryNote(HttpServletRequest request){
        Map<String, List<Notebook>> map = new HashMap<>();
        HttpSession session = request.getSession(false);
        if(session == null){
            map.put("err",null);
            return map;
        }else {
            User user = (User) session.getAttribute("User");
            List<Notebook> notebooks = notebookService.selectNoteByAuthor(user.getName());
            map.put("resp",notebooks);
            return map;
        }

    }
}
